﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000345 RID: 837
	internal class WithLatestFromObservable<TLeft, TRight, TResult> : OperatorObservableBase<TResult>
	{
		// Token: 0x06001143 RID: 4419 RVA: 0x0004F6CC File Offset: 0x0004DACC
		public WithLatestFromObservable(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, TRight, TResult> selector) : base(left.IsRequiredSubscribeOnCurrentThread<TLeft>() || right.IsRequiredSubscribeOnCurrentThread<TRight>())
		{
			this.left = left;
			this.right = right;
			this.selector = selector;
		}

		// Token: 0x06001144 RID: 4420 RVA: 0x0004F6FD File Offset: 0x0004DAFD
		protected override IDisposable SubscribeCore(IObserver<TResult> observer, IDisposable cancel)
		{
			return new WithLatestFromObservable<TLeft, TRight, TResult>.WithLatestFrom(this, observer, cancel).Run();
		}

		// Token: 0x04000A32 RID: 2610
		private readonly IObservable<TLeft> left;

		// Token: 0x04000A33 RID: 2611
		private readonly IObservable<TRight> right;

		// Token: 0x04000A34 RID: 2612
		private readonly Func<TLeft, TRight, TResult> selector;

		// Token: 0x02000346 RID: 838
		private class WithLatestFrom : OperatorObserverBase<TResult, TResult>
		{
			// Token: 0x06001145 RID: 4421 RVA: 0x0004F70C File Offset: 0x0004DB0C
			public WithLatestFrom(WithLatestFromObservable<TLeft, TRight, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001146 RID: 4422 RVA: 0x0004F744 File Offset: 0x0004DB44
			public IDisposable Run()
			{
				IDisposable disposable = this.parent.left.Subscribe(new WithLatestFromObservable<TLeft, TRight, TResult>.WithLatestFrom.LeftObserver(this));
				SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
				singleAssignmentDisposable.Disposable = this.parent.right.Subscribe(new WithLatestFromObservable<TLeft, TRight, TResult>.WithLatestFrom.RightObserver(this, singleAssignmentDisposable));
				return StableCompositeDisposable.Create(disposable, singleAssignmentDisposable);
			}

			// Token: 0x06001147 RID: 4423 RVA: 0x0004F792 File Offset: 0x0004DB92
			public override void OnNext(TResult value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06001148 RID: 4424 RVA: 0x0004F7A4 File Offset: 0x0004DBA4
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001149 RID: 4425 RVA: 0x0004F7DC File Offset: 0x0004DBDC
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000A35 RID: 2613
			private readonly WithLatestFromObservable<TLeft, TRight, TResult> parent;

			// Token: 0x04000A36 RID: 2614
			private readonly object gate = new object();

			// Token: 0x04000A37 RID: 2615
			private volatile bool hasLatest;

			// Token: 0x04000A38 RID: 2616
			private TRight latestValue = default(TRight);

			// Token: 0x02000347 RID: 839
			private class LeftObserver : IObserver<TLeft>
			{
				// Token: 0x0600114A RID: 4426 RVA: 0x0004F814 File Offset: 0x0004DC14
				public LeftObserver(WithLatestFromObservable<TLeft, TRight, TResult>.WithLatestFrom parent)
				{
					this.parent = parent;
				}

				// Token: 0x0600114B RID: 4427 RVA: 0x0004F824 File Offset: 0x0004DC24
				public void OnNext(TLeft value)
				{
					if (this.parent.hasLatest)
					{
						TResult value2 = default(TResult);
						try
						{
							value2 = this.parent.parent.selector(value, this.parent.latestValue);
						}
						catch (Exception error)
						{
							object gate = this.parent.gate;
							lock (gate)
							{
								this.parent.OnError(error);
							}
							return;
						}
						object gate2 = this.parent.gate;
						lock (gate2)
						{
							this.parent.OnNext(value2);
						}
					}
				}

				// Token: 0x0600114C RID: 4428 RVA: 0x0004F8FC File Offset: 0x0004DCFC
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.OnError(error);
					}
				}

				// Token: 0x0600114D RID: 4429 RVA: 0x0004F944 File Offset: 0x0004DD44
				public void OnCompleted()
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.OnCompleted();
					}
				}

				// Token: 0x04000A39 RID: 2617
				private readonly WithLatestFromObservable<TLeft, TRight, TResult>.WithLatestFrom parent;
			}

			// Token: 0x02000348 RID: 840
			private class RightObserver : IObserver<TRight>
			{
				// Token: 0x0600114E RID: 4430 RVA: 0x0004F98C File Offset: 0x0004DD8C
				public RightObserver(WithLatestFromObservable<TLeft, TRight, TResult>.WithLatestFrom parent, IDisposable subscription)
				{
					this.parent = parent;
					this.selfSubscription = subscription;
				}

				// Token: 0x0600114F RID: 4431 RVA: 0x0004F9A2 File Offset: 0x0004DDA2
				public void OnNext(TRight value)
				{
					this.parent.latestValue = value;
					this.parent.hasLatest = true;
				}

				// Token: 0x06001150 RID: 4432 RVA: 0x0004F9C0 File Offset: 0x0004DDC0
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.OnError(error);
					}
				}

				// Token: 0x06001151 RID: 4433 RVA: 0x0004FA08 File Offset: 0x0004DE08
				public void OnCompleted()
				{
					this.selfSubscription.Dispose();
				}

				// Token: 0x04000A3A RID: 2618
				private readonly WithLatestFromObservable<TLeft, TRight, TResult>.WithLatestFrom parent;

				// Token: 0x04000A3B RID: 2619
				private readonly IDisposable selfSubscription;
			}
		}
	}
}
